using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using GarageGames.Torque.Core;

namespace Xyne.GUI.GUIControls.Menu
{
    public class MenuManager
    {
        #region Members
        public delegate void CompletelyBackedOutDelegate();

        private Vector2 _menuPosition;
        private Vector2 _cookieCrumbPosition;
        private CircleMenu _root;
        private Vector2 CookieCrumbOffset = new Vector2(10, 0);

        private Stack<CircleMenu> _cookieCrumbs;
        private List<CircleMenu> _menus;

        private CircleMenu _currentMenu;

        private CompletelyBackedOutDelegate _completelyBackedOut;

        private bool visible;
        #endregion

        #region Properties
        public CircleMenu Root
        {
            get
            {
                return _root;
            }
        }

        public Vector2 MenuPosition
        {
            get
            {
                return _menuPosition;
            }

            set
            {
                _menuPosition = value;
            }
        }

        public CompletelyBackedOutDelegate CompletelyBackedOut
        {
            get
            {
                return _completelyBackedOut;
            }

            set
            {
                _completelyBackedOut = value;
            }
        }

        public CircleMenu Current
        {
            set
            {
                if (_currentMenu != null)
                {
                    _currentMenu.DisableInputMap();
                    _currentMenu.Hide();     
                }

                _currentMenu = value;
                if (value != null)
                {
                    _currentMenu.EnableInputMap();
                    _currentMenu.Position = _menuPosition;
                    _currentMenu.Manager = this;
                    _currentMenu.Show();
                }
            }

            get
            {
                return _currentMenu;
            }
        }

        public bool Visible
        {
            get
            {
                return visible;
            }

            set
            {
                if (visible != value)
                {
                    foreach (MenuItem menuItem in _cookieCrumbs)
                    {
                        menuItem.Visible = value;
                    }

                    if (Current != null)
                    {
                        Current.Visible = value;
                        if (Current.Visible)
                        {
                            Current.Show();
                        }
                        else
                        {
                            Current.Hide();
                        }
                    }
                }
                visible = value;
            }
        }
        #endregion

        #region Constructors
        public MenuManager(Vector2 menuPosition, Vector2 cookieCrumbPosition)
        {
            _menus = new List<CircleMenu>();
            _menuPosition = menuPosition;
            _cookieCrumbPosition = cookieCrumbPosition;
            _cookieCrumbs = new Stack<CircleMenu>();
        //    _root = root;
      //      root.Position = _menuPosition;
         //   _root.Manager = this;
       //     TorqueObjectDatabase.Instance.Register(root);
            visible = false;
        }
        #endregion

        #region Method
        public void PushMenu(CircleMenu menu)
        {
            _cookieCrumbs.Push(menu);
            menu.Position = _cookieCrumbPosition + CookieCrumbOffset * _cookieCrumbs.Count;

        }

        public void CancelMenu(CircleMenu menu)
        {
            if (_cookieCrumbs.Count > 0)
            {
                CircleMenu tempMenu = _cookieCrumbs.Pop();
                Current = tempMenu;
            }
            else
            {
                if (_completelyBackedOut != null)
                {
                    _completelyBackedOut();
                }
            }
        }

        public void ShowRoot()
        {

            Current = _root;
            Visible = true;
        }

        public void AddMenu(CircleMenu circleMenu)
        {
            _menus.Add(circleMenu);
        }

        public void RefreshMenus()
        {
            if (_menus != null)
            {
                foreach (CircleMenu menu in _menus)
                {
                    menu.RefreshMenuItems();
                }
            }
        }
        #endregion


    }
}
